/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-13
 * Time:23:19
 */

/**
 * 力扣670:最大交换数
 */

public class maximumSwap {
    public int maximumSwap(int num) {
        //先将num转变为数组
        char[] charArray = String.valueOf(num).toCharArray();
        int n = charArray.length;
        int maxNum = num;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                //交换
                swap(charArray, i, j);
                //一直进行比较直到选出最大的一个
                //integer.parseint（)是将整型数据Integer转换为基本数据类型int
                maxNum = Math.max(maxNum, Integer.parseInt(new String(charArray)));
                swap(charArray, i, j);
            }
        }
        return maxNum;
    }

    public void swap(char[] charArray, int i, int j) {
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
    }
}
